﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SoftHotel.Models;
using System.Data;
using System.Data.Entity;
using PagedList;

namespace SoftHotel.Controllers
{
    public class ReservaEvController : Controller
    {
        //
        // GET: /ReservaEv/
        ReservaEvDAO resEvDAO = new ReservaEvDAO();
        DetalleReservaEvDAO detResDAO = new DetalleReservaEvDAO();
        InvitadoDAO invDAO = new InvitadoDAO();
        ClienteDAO cliDAO = new ClienteDAO();

        
        
        public ActionResult Index()
        {

            List<ReservaEv> reservaEvs = new List<ReservaEv>();
            reservaEvs = resEvDAO.ejecutar_query();
            Response.AddHeader("Refresh", "30");
            return View(reservaEvs);
        }


        // GET: /Servicio/Buscar

        public ActionResult Search()
        {
            ReservaEv reservEv = new ReservaEv();
            reservEv.fechaCreacion = System.DateTime.Today.ToShortDateString();
            
            return View(reservEv);
        }


        [HttpPost]
        public ActionResult Search(ReservaEv bus_reserva)
        {
            List<ReservaEv> reservaEvs = new List<ReservaEv>();

            switch (bus_reserva.nomEstado)
            {
                case "0": bus_reserva.estado = 0; break;
                case "1": bus_reserva.estado = 1; break;
                case "2": bus_reserva.estado = 2; break;
                case "3": bus_reserva.estado = 3; break;
                case "4": bus_reserva.estado = 4; break;
                case "5": bus_reserva.estado = 5; break;
            }
            reservaEvs = resEvDAO.ejecutar_busqueda(bus_reserva);
            return View("Index", reservaEvs);
        }
        
        
        // GET: /ReservaEv/Details/5


        public ActionResult Details(Int32 idReserva)
        {

            ReservaEv reservaEv = new ReservaEv();
            reservaEv = resEvDAO.consultar_reservaEv(idReserva);
            return View(reservaEv);
        }

        [HttpGet]
        public ActionResult _vistaParcial(int id)
        {
            List<DetalleReservaEv> detReservaEvs = new List<DetalleReservaEv>();
            detReservaEvs = detResDAO.ejecutar_query(id);
            return PartialView("_detalleIndex",detReservaEvs);
        }


        [HttpGet]
        public ActionResult BuscarCliente()
        {
            var clientesAux = cliDAO.clientes.Include(a => a.objeTipoDoc).Include(a => a.objEstado).Include(a => a.objTipoCliente);

            clientesAux = clientesAux.OrderByDescending(s => s.apellidoPaterno.ToLower());
            int tamanhoPagina = 5;
            int NumPagina = 1;
            return PartialView("BuscarCliente",clientesAux.ToPagedList(NumPagina, tamanhoPagina));
        }


        //public ActionResult BuscarCliente(PagedList.IPagedList<SoftHotel.Models.Cliente> cliente)
        [HttpPost]
        public ActionResult BuscarCliente(Cliente cliente)
        {
            return Content(cliente.idCliente.ToString());
        }

        [HttpGet]
        public ActionResult Invitados(Int32 idReserva)
        {

            ReservaEv reservaEv = new ReservaEv();
            reservaEv = resEvDAO.consultar_reservaEv(idReserva);
            return View(reservaEv);
        }

        [HttpGet]
        public ActionResult _vistaInvitados(int id)
        {
            List<Invitado> lista = new List<Invitado>();
            lista =  invDAO.ejecutar_query(id);
            return PartialView("_listaInvitados", lista);
        }






        //
        // GET: /ReservaEv/Create

        public ActionResult Create()
        {

            ReservaEv reservaEv = new ReservaEv();

            
            
            return View(reservaEv);
            
        } 

        //
        // POST: /ReservaEv/Create

        [HttpPost]
        public ActionResult Create(ReservaEv reservaEv)
        {
            
           
            try
            {
                 //TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    resEvDAO.add_reservaEv(reservaEv);

                    //prueba promocion
                    //DetallePromocionDAO promoDAO = new DetallePromocionDAO();
                   // promoDAO.generar_promo(reservaEv.cliente, reservaEv.idReserva);

                    List<DetalleReservaEv> detReservaEvs = new List<DetalleReservaEv>();
                    detReservaEvs = detResDAO.ejecutar_query(reservaEv.idReserva);
                    
                    return RedirectToAction("Details",reservaEv);

                }
                return View(reservaEv);
            }
            catch
            {
                return View(reservaEv);
            }
        }



             
        
        //
        // GET: /ReservaEv/Edit/5
 
        public ActionResult Edit(Int32 id)
        {
            ReservaEv reservaEv = new ReservaEv();
            reservaEv = resEvDAO.consultar_reservaEv(id);

            switch (reservaEv.estado)
            {
                case 0: reservaEv.nomEstado = "Reservada"; break;
                case 1: reservaEv.nomEstado = "Confirmada"; break;
                case 2: reservaEv.nomEstado = "Iniciada"; break;
                case 3: reservaEv.nomEstado = "Finalizada"; break;
                case 4: reservaEv.nomEstado = "Anulada"; break;
            }

           

            return View(reservaEv);
        }

        //
        // POST: /ReservaEv/Edit/5

        [HttpPost]
        public ActionResult Edit(Int32 id, ReservaEv reservaEv)
        {
            
            try
            {
                // TODO: Add update logic here

                switch (reservaEv.nomEstado)
                {
                    case "0": reservaEv.estado =0 ; break;
                    case "1": reservaEv.estado = 1; break;
                    case "2": reservaEv.estado = 2; break;
                    case "3": reservaEv.estado = 3; break;
                    case "4": reservaEv.estado =4 ; break;
                }
                resEvDAO.editar_reservaEv(id, reservaEv);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(reservaEv);
            }
        }

        //
        // GET: /ReservaEv/Delete/5
 
        public ActionResult Delete(Int32 id)
        {
            ReservaEv reservaEv = new ReservaEv();
            reservaEv = resEvDAO.consultar_reservaEv(id);
            return View(reservaEv);
        }

        //
        // POST: /ReservaEv/Delete/5

        [HttpPost]
        public ActionResult Delete(Int32 id, ReservaEv reservaEv)
        {
            try
            {
                // TODO: Add delete logic here
                resEvDAO.eliminar_reservaEv(id, reservaEv);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
